import 'dart:convert';
import 'dart:io';
import 'package:base_flutter/base_flutter.dart';

import 'package:flutter_local_notifications/flutter_local_notifications.dart';

import '../a_yyt/page/policy_remine_page.dart';

class NotifycationUtils{

  NotifycationUtils._();

  var count = 0;

  static NotifycationUtils? _instance;

  static NotifycationUtils get instance => _instance??=NotifycationUtils._();

  FlutterLocalNotificationsPlugin? flutterLocalNotificationsPlugin;

  void init(){
    flutterLocalNotificationsPlugin =
    new FlutterLocalNotificationsPlugin();

    if(Platform.isAndroid){
      flutterLocalNotificationsPlugin?.resolvePlatformSpecificImplementation<
          AndroidFlutterLocalNotificationsPlugin>()!.requestPermission();
    }

    var initializationSettingsAndroid =
    new AndroidInitializationSettings('报警通知');

    // var initializationSettingsIOS =
    //     IOSInitializationSettings(onDidReceiveLocalNotification: null);

    var initializationSettings = InitializationSettings(
        android: initializationSettingsAndroid,
        iOS: DarwinInitializationSettings());

    flutterLocalNotificationsPlugin?.initialize(initializationSettings,onDidReceiveNotificationResponse: onSelectNotification);
  }

  void showSosMessage(String message){
    var iOSPlatformChannelSpecifics =
    DarwinNotificationDetails(presentAlert: true,presentBadge: true,presentSound: true);
    var androidPlatformChannelSpecifics = AndroidNotificationDetails(
      '报警通知',
      '报警通知',
      channelDescription: '报警通知',
      importance: Importance.max,
      priority: Priority.high,playSound: true,
      ticker: '智慧养老院',
    );

    var platformChannelSpecifics = NotificationDetails(
      android: androidPlatformChannelSpecifics,
      iOS: iOSPlatformChannelSpecifics,
    );
    flutterLocalNotificationsPlugin?.show(count, '报警提醒', message, platformChannelSpecifics);
    count++;
    if(count>100){
      count = 0;
    }
  }

  static Future onSelectNotification(NotificationResponse details) async {
    Go().push(PolicyReminePage());
  }
}